import { registerDecorator,ValidationArguments,ValidationOptions } from "class-validator";
import { PrismaClient } from "@prisma/client";
// 表字段是否唯一
export function IsNotExistsRule(
    table:string,
    validationOptions?:ValidationOptions
){
    return function (object:Record<string,any>,propertyName:string){
        registerDecorator({
            name:'IsNotExistsRule',
            target:object.constructor,
            propertyName:propertyName,
            constraints:[table],
            options:validationOptions,
            validator:{
                async validate(value:string,args:ValidationArguments) {
                    const prisma = new PrismaClient()
                    const user = await prisma[table].findFirst({
                        where:{
                            [propertyName]:args.value,
                        }
                    })
                    console.log(user);
                    return !Boolean(user);
                }
            }
        })
    }
}